package com.example.study.design.factory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 用简单方法模式实现: 工厂的工厂，作用是不需要每次创建新的工厂对象
 *
 * @author duckquan
 */
@Component
public class KeyMethodFactoryMap {

    @Autowired
    private BluetoothKeyFactory bluetoothKeyFactory;

    @Autowired
    private ShareKeyFactory shareKeyFactory;

    @Autowired
    private WatchKeyServiceFactory watchKeyServiceFactory;

    //存放具体工厂对象的map集合
    private static final Map<Integer, KeyMethodFactory> cachedFactories = new HashMap<>();

    @PostConstruct
    public void init() {
        cachedFactories.put(1, bluetoothKeyFactory);
        cachedFactories.put(2, shareKeyFactory);
        cachedFactories.put(3, watchKeyServiceFactory);
    }

    /**
     * 获得具体工厂类
     *
     * @param type
     * @return
     */
    public KeyMethodFactory getFactory(Integer type) {
        return cachedFactories.get(type);
    }

}
